home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / embedded / m68k / fbug68k.arc / PRINTUN.IXC < prev    next >
Text File  |  1989-09-29  |  5KB  |  154 lines

  1. #include "userdef.h"
  2. #include <varargs.h>
  3.  
  4. int print(p,va_alist)
  5. char *p;                /* pointer to format string            */
  6. va_dcl
  7. {
  8. va_list params;
  9. char **pfmt;
  10. char pch;
  11. char *pformat;          /* pointer to format string */
  12. int pargument;  /* pointer to arg's for fmt string */
  13. int pvalue;
  14. char pchar;
  15. int pi,pj, pk;
  16. int pmaxwidth,pmaxpoints;
  17. char pstr[MAXLINE];
  18. unsigned ptemp;
  19.  
  20.         va_start(params);
  21.         pformat = p;    /* set "format" to the format string */
  22.  
  23. /*      Depending on the machine being used the variable number of parameters
  24.         passed to this function will not necessarily be accessed by incrementing
  25.         the pointer.  So a variable argument function will have to be used  */
  26.  
  27.  
  28.         while (pch = *pformat++)
  29.         {
  30.                 if (pch == '%')
  31.                 {
  32.                         pch = *pformat++;
  33.                         pi = 1;
  34.                         pmaxwidth = 0;
  35.                         pmaxpoints = 0;
  36.                         while ((pch >= '0') && (pch <= '9'))
  37.                         {
  38.                                 pmaxwidth = (pch - '0') * pi + pmaxwidth;
  39.                                 pi = pi * 10;
  40.                                 pch = *pformat++; 
  41.                         }
  42.                         if (pch == '.')
  43.                         {
  44.                                 pch = *pformat++;
  45.                                 while ((pch >= '0') && (pch <= '9'))
  46.                                 {
  47.                                         pmaxpoints = (pch - '0') * pi + pmaxpoints;
  48.                                         pi = pi * 10;
  49.                                         pch = *pformat++;
  50.                                 }
  51.                         }
  52.             *pformat--;
  53.             pch = *pformat++;
  54.                         switch (pch)
  55.                         {
  56.                         case 'd':
  57.                         case 'u':
  58.                         case 'x':
  59.                         case 'X':
  60.                                 pvalue = va_arg(params,int);
  61.                                 if (pch == 'd' && pvalue < 0)
  62.                                 {
  63.                                         putch(TERMINAL,'-');
  64.                                         pvalue = -pvalue;
  65.                                 }
  66.  
  67.                                 if (pch == 'X' && pvalue < 0)
  68.                                 {
  69.                                         pvalue = -pvalue;
  70.                                 }
  71.  
  72.                                 if ((pch == 'd') || (pch == 'u'))
  73.                                         pk = 10;
  74.                                 else
  75.                                         pk = 16;
  76.                                 pi = 0;
  77.                                 ptemp = pvalue;
  78.  
  79.                                 do
  80.                                 {
  81.                                         pstr[pi] = (ptemp % pk) + '0';
  82.                                         if (pstr[pi] > '9')
  83.                                                 pstr[pi] = pstr[pi] - ':' + 'A';
  84.                                         pi++;
  85.                                 }
  86.                                 while ((ptemp = ptemp / pk) > 0);
  87.  
  88.                                 pstr[pi] = ENDSTR;
  89.                                 pj = --pi;
  90.                                 for (pk = 0; pk < pj; pk++)
  91.                                 {
  92.                                         pch = pstr[pk];
  93.                                         pstr[pk] = pstr[pj];
  94.                                         pstr[pj--] = pch;
  95.                                 }
  96.  
  97.                 if(pmaxwidth != 0)
  98.                 {
  99.                                     pmaxwidth = pmaxwidth - pi - 1;
  100.                                        while (pmaxwidth > 0)
  101.                                        {
  102.                                                putch(TERMINAL,'0');
  103.                                                pmaxwidth--;
  104.                                        }
  105.                     pi = 0;
  106.                                        while (pmaxwidth < 0)
  107.                                      {
  108.                                                pmaxwidth++;
  109.                         pi++;
  110.                                        }
  111.                 }
  112.                 else
  113.                     pi = 0;
  114.  
  115.                                 for (;pstr[pi] != ENDSTR;pi++)
  116.                                         putch(TERMINAL,pstr[pi]);
  117.                                 break;
  118.  
  119.                         case 'c':
  120.                                 pchar = va_arg(params,int);
  121.                                 putch(TERMINAL,pchar);
  122.                                 break;
  123.         
  124.                         default:
  125.                                 pformat--;  /* need to back up if unrecognized */
  126.                                 putch(TERMINAL,'%');
  127.  
  128.                         }               /* switch c */
  129.  
  130.                 }
  131.                 else if (pch == '\n')
  132.                 {
  133.                         putch(TERMINAL,CR);     /* cr */
  134.                         putch(TERMINAL,LF);     /* lf */
  135.                 }
  136.                 else
  137.                 {
  138.                         putch(TERMINAL,pch); 
  139.                 }
  140.         } 
  141.         va_end(params);
  142.   }
  143.  
  144. /* ************************************************************ */
  145.  
  146. putch(addr,data)
  147. int addr,data;
  148. {
  149.         putchar(data);
  150. }
  151.  
  152. /* ************************************************************ */
  153.  
  154.